//
//  GJFPlotTableViewCell.m
//  Wedding
//
//  Created by 郭静芬 on 2016/12/21.
//  Copyright © 2016年 郭静芬. All rights reserved.
//

#import "GJFPlotTableViewCell.h"

@interface GJFPlotTableViewCell ()
@property (nonatomic, strong)UIImageView *imageV;
@property (nonatomic, strong)UILabel *title;
@property (nonatomic, strong)UILabel *category_name;
@property (nonatomic, strong)UILabel *collect;
@property (nonatomic, strong) UIView *collectBgView;
@property (nonatomic, strong) UIButton *imageCollect;
@end

@implementation GJFPlotTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)setPlotModel:(GJFPlotModel *)plotModel {
    _plotModel = plotModel;
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:plotModel.cover_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
    self.title.text = plotModel.title;
    self.category_name.text = plotModel.category_name;
    self.collect.text = [NSString stringWithFormat:@"收藏：%@", plotModel.collectors_count];
    [self collectBgView];
    self.imageCollect.selected = [plotModel.is_collect boolValue];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageV.frame = CGRectMake(0, 0, WIDTH, 200 * YY);
    self.title.frame = CGRectMake(10, self.imageV.frame.size.height + 10 * XX, WIDTH, 25 * YY);
    self.category_name.frame = CGRectMake(10 * XX, self.title.frame.size.height + self.title.frame.origin.y, 200 * XX, 30 * YY);
    self.collect.frame = CGRectMake(WIDTH - 160 * XX, self.category_name.frame.origin.y, 150 * XX, 20 * YY);
    self.collectBgView.frame = CGRectMake(self.contentView.frame.size.width - 60 * XX, 15 * YY, 40 * YY, 40 * YY);
    self.imageCollect.frame = CGRectMake(self.contentView.frame.size.width - 55 * XX, 21 * YY, 30 * YY, 30 * YY);

}
//点击右上角的五角星进行收藏/取消收藏
- (void)collectAction {
    NSDictionary *dict = @{@"collectId" : self.plotModel.id?:@"", @"type":self.plotModel.type};
    NSString *url = self.imageCollect.isSelected?APP_PLOT_UNCOLLECT:APP_PLOT_COLLECT;
    [GJFNetworkTool POSTRequestWithUrl:url params:dict complete:^(NSDictionary *result) {
        if ([result[@"code"] intValue] == 1000) {
            self.imageCollect.selected = !self.imageCollect.selected;
            self.plotModel.is_collect = [NSString stringWithFormat:@"%d", ![self.plotModel.is_collect boolValue]];
        }
        [[[GJFSingleTool topViewController] view] showWarning:result[@"msg"]];
    }];
}

#pragma mark - 懒加载
- (UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        _imageV.backgroundColor = [UIColor lightGrayColor];
        _imageV.contentMode = UIViewContentModeScaleAspectFill;
        _imageV.clipsToBounds = YES;
        [self.contentView addSubview:_imageV];
    }
    return _imageV;
}
- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.font = [UIFont systemFontOfSize:18 * XX];
        [self.contentView addSubview:_title];
    }
    return _title;
}
- (UILabel *)category_name {
    if (!_category_name) {
        _category_name = [[UILabel alloc] init];
        _category_name.font = [UIFont systemFontOfSize:15 * XX];
        _category_name.textColor = [UIColor grayColor];
        [self.contentView addSubview:_category_name];
    }
    return _category_name;
}
- (UILabel *)collect {
    if (!_collect) {
        _collect = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 150, 230 * YY, 130 * XX, 30 * YY)];
        _collect.font = [UIFont systemFontOfSize:15 * XX];
        _collect.textColor = [UIColor grayColor];
        _collect.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_collect];
    }
    return _collect;
}
// 收藏按钮背景透明
- (UIView *)collectBgView {
    if (!_collectBgView) {
        _collectBgView = [[UIView alloc] init];
        _collectBgView.backgroundColor = [UIColor blackColor];
        _collectBgView.alpha = 0.5;
        _collectBgView.layer.cornerRadius = 20 * YY;
        [self.contentView addSubview:_collectBgView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectAction)];
        [_collectBgView addGestureRecognizer:tap];
    }
    return _collectBgView;
}

- (UIButton *)imageCollect {
    if (!_imageCollect) {
        _imageCollect = [UIButton buttonWithType:UIButtonTypeCustom];
        [_imageCollect setImage:[UIImage imageNamed:@"未收藏"] forState:UIControlStateNormal];
        [_imageCollect setImage:[UIImage imageNamed:@"已收藏"] forState:UIControlStateSelected];
        _imageCollect.userInteractionEnabled = NO;
        [self.contentView addSubview:_imageCollect];
    }
    return _imageCollect;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
